Python 爬虫——Requests基本用法
Python 爬虫——Requests基本用法
ex.1 爬取搜索页面
任务
爬取搜狗搜索的结果
分析
首先搜狗搜索的网址为:https://www.sogou.com/
随便输入一个关键词,例如输入python
,发现网址变成https://www.sogou.com/web?query=python&_asf=www.sogou.com
注意后面出现的?query=python
,不难发现,query后面跟的就是搜索的关键词。
按F11选择网络
页面,刷新页面,选择其中一个文件,查看到以下信息:
请求方法:get
Content-Type:
text/html; charset=utf-8
User-Agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.58
负载:
query: python
捕获到以上信息之后,可以编写爬虫程序
代码
1 | import requests |
Python 爬虫——Requests基本用法